<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl 
 *  All Right Reserved
 ***********************/

namespace Gotcha;

use Gotcha\App\Extensions\ErrorsExtension;
use Gotcha\App\GotchaBootstrap;
use Gotcha\App\GotchaEngine;

error_reporting(E_ALL);

// System version
define('GOTCHA_SYSTEM_VERSION', '1.0-a28');

// Check current request type
$requestType = $_SERVER['REQUEST_METHOD'];
switch($requestType) {
    case 'PUT':
        $requestType = 'PUT';
        break;
    case 'POST':
        $requestType = 'POST';
        break;
    case 'GET':
        $requestType = 'GET';
        break;
    case 'HEAD':
        $requestType = 'HEAD';
        break;
    case 'DELETE':
        $requestType = 'DELETE';
        break;
    case 'OPTIONS':
        $requestType = 'OPTIONS';
        break;
    default:
        $requestType = 'UNKNOWN';
        break;
}

if($requestType == 'UNKNOWN'){
    exit;
}

// Let's make PHP_SELF more secure to prevent XSS attacks
$phpSelf = filter_input(INPUT_SERVER, 'PHP_SELF', FILTER_SANITIZE_URL);

// Define absolute path
$pathInfo = pathinfo($phpSelf); // Get current path info
$basePath = explode('/', $pathInfo['dirname']); // Split path to remove blank first name and filename

// Remove blank first item
array_shift($basePath);

// To clean-up whole $basePath we have to find when
// we reach index.php element in an array
foreach($basePath as $item) {
    if($item === 'index.php') {
        // Save current index
        $currentIndex = array_search('index.php', $basePath);
    }

    // Unset all elements >= $currentIndex
    if(isset($currentIndex) && !empty($currentIndex)) {
        unset($basePath[array_search($item, $basePath)]);
    }
}

// Restore indexes
array_values($basePath);

// Our application base path
define('GOTCHA_BASE_PATH', '/'.implode('/', $basePath));
define('GOTCHA_ROOT_DIR', dirname(__FILE__));

// Since we have a lot of include paths, we should clean it up a bit
$includePaths = array(GOTCHA_ROOT_DIR.'/App/Extensions/', GOTCHA_ROOT_DIR.'/App/Repositories/', GOTCHA_ROOT_DIR.'/App/Database/classes/gotcha/', GOTCHA_ROOT_DIR.'/App/Database/classes/gotcha/om/', GOTCHA_ROOT_DIR.'/App/Database/classes/gotcha/map/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/config/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/util/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/om/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/map/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/exception/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/connection/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/adapter/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/query/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/parser/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/parser/yaml/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/collection/', GOTCHA_ROOT_DIR.'/App/Libs/Propel/runtime/lib/formatter/', GOTCHA_ROOT_DIR.'/App/Types/');

// Update include path
set_include_path(implode(PATH_SEPARATOR, $includePaths) . PATH_SEPARATOR . get_include_path());

// Include bootstrap and engine
require_once 'App/GotchaBootstrap.php';
require_once 'App/GotchaEngine.php';

$init = new GotchaBootstrap();
$init->init();

$run = new GotchaEngine();
$run->run();
?>